
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;


public class DetailsPaneController {
  private @FXML TextField firstNameTextField ;
  private @FXML TextField lastNameTextField ;
  private ObjectProperty<Person> personProperty ;
  
  public void initialize() {
    personProperty = new SimpleObjectProperty<Person>(null) ;
    
    personProperty.addListener(new ChangeListener<Person>() {

      @Override
      public void changed(ObservableValue<? extends Person> observable,
          Person oldValue, Person newValue) {

        if (oldValue != null) {
          firstNameTextField.textProperty().unbindBidirectional(oldValue.firstNameProperty());
          lastNameTextField.textProperty().unbindBidirectional(oldValue.lastNameProperty());
        }
        if (newValue != null) {
          firstNameTextField.textProperty().bindBidirectional(newValue.firstNameProperty());
          lastNameTextField.textProperty().bindBidirectional(newValue.lastNameProperty());
        }
      }
      
    });
  }
  
  public ObjectProperty<Person> personProperty() {
    return personProperty ;
  }
  
  public Person getPerson() {
    return personProperty.get();
  }
  
  public void setPerson(Person p) {
    personProperty.set(p);
  }
}